/**
 * 
 */
package jedis;

import java.util.HashSet;
import java.util.Set;

//import java.util.HashSet;
//import java.util.Set;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import freeconfig.GetProperties;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Description:  集群版
 * @version: v1.0.0
 * @author: wbl
 * @date: 2019年8月27日 下午3:53:22
 */
public class JedisBean {
	// 集群的配置

	private String clusterNodes=GetProperties.getValue("redis.cluster.node");
	private String password=GetProperties.getValue("redis.cluster.password");
	private Integer database=Integer.parseInt(GetProperties.getValue("redis.cluster.database"));
	private Integer timeout=Integer.parseInt(GetProperties.getValue("redis.cluster.timeout"));
	private Integer maxIdle=Integer.parseInt(GetProperties.getValue("redis.jedis.pool.max-idle"));
	private Integer minIdle=Integer.parseInt(GetProperties.getValue("redis.jedis.pool.min-idle"));
	private Integer maxTotal=Integer.parseInt(GetProperties.getValue("redis.jedis.pool.max-active"));
	private Integer maxWaitMillis=Integer.parseInt(GetProperties.getValue("redis.jedis.pool.max-wait"));
	
	@Bean
	public JedisCluster getJedisCluster() {
		String[] cNodes = clusterNodes.split(",");
		Set<HostAndPort> nodes = new HashSet<>();
		for (String node : cNodes) {
			String[] hp = node.split(":");
			nodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
		}
		return new JedisCluster(nodes, maxWaitMillis, 1000, 3, password, getPoolConfig());
	}
	
	public JedisPoolConfig getPoolConfig(){
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		poolConfig.setMaxIdle(maxIdle);
		poolConfig.setMaxWaitMillis(maxWaitMillis);
		return poolConfig;
	}
		
}
